<?php

/**
 * This is the model class for table "{{user_level}}".
 *
 * The followings are the available columns in table '{{user_level}}':
 * @property integer $id
 * @property string $name
 * @property string $logo
 * @property integer $need_points
 * @property integer $is_open
 * @property integer $listorder
 */
class UserLevel extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user_level}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, need_points', 'required'),
			array('need_points, is_open, listorder', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>20),
			array('logo', 'length', 'max'=>120),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, logo, need_points, is_open, listorder', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'logo' => 'Logo',
			'need_points' => 'Need Points',
			'is_open' => 'Is Open',
			'listorder' => 'Listorder',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('logo',$this->logo,true);
		$criteria->compare('need_points',$this->need_points);
		$criteria->compare('is_open',$this->is_open);
		$criteria->compare('listorder',$this->listorder);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return UserLevel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public function getList($page,$rows,$order,$sort,$username){
        $db = Yii::app()->db->createCommand();
        $db->select('*');
        $db->from('{{user_level}}');
        if ($username) {
            $db->andWhere(array('like', 'name', '%'.$username.'%'));
        }
        $db->order($sort.' '.$order);
        $db->limit($rows, ($rows * ($page - 1)));
        $info = $db->queryAll();
        foreach($info as $key=>$value){
            $info[$key]['is_open'] = $value['is_open'] ? '<i  onclick="level_tool.setStatus(\''.$value['id'].'\',0)"  style="padding: 6px;" class="icon-bullet_tick"></i>' : '<i  onclick="level_tool.setStatus(\''.$value['id'].'\',1)"  class="icon-bullet_cross"  style="padding: 6px;" ></i>';
        }
        return array(
            'rows'=>$info ? $info : '',
            'total'=>$this->getTotal($username)
        );
    }
    private function getTotal($username){
        $db = Yii::app()->db->createCommand();
        $db->select('COUNT(*) AS count');
        $db->from('{{user_level}}');
        if ($username) {
            $db->andWhere(array('like', 'name', '%'.$username.'%'));
        }
        $info = $db->queryRow();
        return $info['count'];
    }
    //删除
    public function remove($ids){
        return $this->deleteAll('id in('.$ids.')');
    }
}
